package jbdc;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import jdbc2.User;
import jdbc3.Brand;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @author 路老师
 * @date 2025/3/12
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 * 数据库操作类
 */
public class MyHandleDBNice {
    public Connection connection;

    public MyHandleDBNice() {
        this.conn();
    }

    /**
     * 获取德鲁伊数据库池连接对象
     */
    private void conn() {
        try {
            Properties properties = new Properties();
            FileInputStream fileInputStream = new FileInputStream("src/jdbc/DruidJdbc.properties");
            properties.load(fileInputStream);

            //使用德鲁伊数据库连接池技术
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

            this.connection = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有数据
     *
     * @param sql 查询语句
     * @return 数据集合
     */
    public ArrayList<Brand> findAllData(String sql) {
        ArrayList<Brand> brands = null;
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            brands = new ArrayList<>();
            while (resultSet.next()) {
                Brand brand = new Brand();
                brand.setId(resultSet.getInt("stu_id"));
                brand.setName(resultSet.getString("stu_name"));
                brand.setAge(resultSet.getString("stu_age"));
                brand.setSex(resultSet.getInt("stu_sex"));
                brand.setPhone(resultSet.getInt("stu_phone"));
                brands.add(brand);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return brands;
    }

    /**
     *
     * @param brand
     * @return
     * @throws SQLException
     */
    public int insert(Brand brand) throws SQLException {
        String sql = "insert into tb_brand values(null,?,?,?,?,?)";
        PreparedStatement prestatement = connection.prepareStatement(sql);
        prestatement.setString(1, brand.getName());
        prestatement.setInt(2, brand.getAge());
        prestatement.setString(3, brand.getSex());
        prestatement.setInt(4, brand.getPhone());
        int i = prestatement.executeUpdate();
        return i;

    }



    public static void main(String[] args) throws SQLException {
        MyHandleDBNice myHandleDBNice = new MyHandleDBNice();
        System.out.println("myHandleDBNice.connection = " + myHandleDBNice.connection);
        ArrayList<Brand> allData = myHandleDBNice.findAllData("select * from tb_brand");
        System.out.println("allData = " + allData);
        Brand brand1 = new Brand(null, "python",  1, "课程设计", 2);
        int insert = myHandleDBNice.insert(brand1);
        System.out.println("插入了 = " + insert+"条数据");
    }
}